﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CRM.BLL.Ali;
using CRM.DAL.Ali;
using CRM.MODEL.Ali;
using CRM.UTIL;

namespace CRM.WEB.Test
{
    public partial class TestPage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            /*RunAliAPI ra = new RunAliAPI();
            //http://119.38.217.38:1680/openapi/param2/1/aliexpress.open/api.queryWarrantiesByOrderId/1688?orderId=60023440513182&supplierId=001&_aop_signature=B0AAB0ADA3A81B1697974919C533582CB2A541FA 
            Dictionary<string, string> dl = new Dictionary<string, string>();
            //dl.Add("orderId", "60023440513182");
            //dl.Add("supplierId", "001");
            dl.Add("page", "1");
            dl.Add("pageSize", "11");
            Response.Write(ra.GoRun("http://gw.api.alibaba.com:80/openapi/",
                "param2/1/aliexpress.open/api.findOrderListQuery/", dl));*/


            //业务操作
            var serv = new RunAliAPI();
            var obj = new BLL.Ali.Ali_OrdersB();
            var stu = new MODEL.Ali.GetAli_Orders
            {
                ORDERSTRING_VALUE = " o.ID Asc",
                CURRENTPAGEINDEX_VALUE = 1,
                PAGESIZE_VALUE = 100000
            };
            int successCount = 0;
            int failCount = 0;
            var sb = new StringBuilder();
            var dt = obj.GetAli_Orders(ref stu);
            for (var i = 0; i < dt.Rows.Count; i++)
            {
                var dr = dt.Rows[i];
                try
                {
                    var entity = new MODEL.Ali.EditAli_Orders
                    {
                        ACTION_VALUE = "Update",
                        ID_VALUE = dr["ID"]
                    };
                    var resJson = serv.GetOrderInfo(dr["OrderId"].ToString(), false, false);

                    var htFirst = JSON.Decode(resJson) as Hashtable;
                    if (htFirst["success"].ToString().ToLower() == "true")
                    {

                        CommonClass.Common.InitDataEntity(htFirst, dr["BizID"].ToString(),
                            false, ref entity);

                        obj.EditAli_Orders(ref entity);


                        successCount++;
                    }
                    else
                    {
                        failCount++;
                        sb.Append("第" + (i + 1) + "行调用Ali接口失败;");
                    }
                }
                catch (Exception ex)
                {
                    failCount++;
                    sb.Append("第" + (i + 1) + "行异常：" + ex.Message);
                }
            }
            Response.Write(string.Format("更新成功行数{0},失败行数{1},出错信息:{2}", successCount, failCount, sb.ToString()));

            //var stu1 = new MODEL.Ali.GetAli_Orders();
            //stu1.ORDERIDVALUE = "71104258291640";
            //stu1.ENIDVALUE = "7109551664";
            //var oDt = obj.GetAli_Orders(ref stu1);
            //Response.Write(oDt.Rows.Count);
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            //RunAliAPI ra = new RunAliAPI();
            //http://119.38.217.38:1680/openapi/param2/1/aliexpress.open/api.queryWarrantiesByOrderId/1688?orderId=60023440513182&supplierId=001&_aop_signature=B0AAB0ADA3A81B1697974919C533582CB2A541FA 
            //Dictionary<string, string> dl = new Dictionary<string, string>();
            //dl.Add("orderId", "71104258281640");
            //dl.Add("supplierId", "wisetech");
            //Response.Write(ra.GoRun("http://119.38.217.38:1680/openapi","param2/1/aliexpress.open/api.queryWarrantiesByOrderId/1688", dl, false, false));
            //Response.Write(ra.GoRun("http://gw.api.alibaba.com:80/openapi","param2/1/aliexpress.open/api.queryWarrantiesByOrderId/4957740", dl, false, false));
            //Response.Write(ra.GetOrderInfo("71104258281640",false,false));

            //var dt = new CRM.DAL.Ali.Ali_OrdersD().GetDataBySql("select * from Ali_Orders where ProductType is null");
            //int c1 = 0;
            //int c2 = 0;
            //int c3 = 0;
            //int c4 = 0;
            //var sb = new StringBuilder();
            //foreach (DataRow dr in dt.Rows)
            //{
            //    try
            //    {
            //        CommonClass.Common.SyncOrderInfo(dr["OrderId"].ToString(), out c1, out c2, out c3, out c4);
            //    }
            //    catch (Exception ex)
            //    {
            //        sb.AppendFormat(dr["OrderId"].ToString() + ex.Message);
            //    }
            //}
            //Response.Write(sb.ToString());
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            var obj=new BLL.Ali.Ali_OrdersB();
            var stu=new MODEL.Ali.GetAli_Orders();
            stu.PAGESIZE_VALUE = 100000;
            stu.CURRENTPAGEINDEX_VALUE = 1;
            stu.EFFECTIVE_VALUE = true;
            var dt = obj.GetAli_Orders(ref stu);
            //var dt = new DAL.Ali.Ali_OrdersD().GetDataBySql("select * from [Ali_Orders] where BizID is null or BizID =''");
            var aliApiServ = new RunAliAPI();
            int successCount=0;
            int failCount=0;
            var sb=new StringBuilder();
            txtInfo.Text += "总数：" + dt.Rows.Count + "\r\n";
            //for (int i = 0; i < dt.Rows.Count; i++)
            //{
            //    var dr = dt.Rows[i];
            //    try
            //    {
            //        var entity = new MODEL.Ali.EditAli_Orders
            //        {
            //            ACTION_VALUE = "Update",
            //            ID_VALUE = dr["ID"]
            //        };
            //        var resJson = aliApiServ.GetOrderInfo(dr["OrderId"].ToString(), false, false);

            //        var htFirst = JSON.Decode(resJson) as Hashtable;
            //        if (htFirst["success"].ToString().ToLower() == "true")
            //        {

            //            CommonClass.Common.InitDataEntity(htFirst, dr["ENid"].ToString(),
            //                false, ref entity);

            //            obj.EditAli_Orders(ref entity);


            //            successCount++;
            //        }
            //        else
            //        {
            //            failCount++;
            //            sb.Append("第" + (i + 1) + "行调用Ali接口失败;");
            //        }
            //    }
            //    catch (Exception ex)
            //    {
            //        failCount++;
            //        sb.Append("第" + (i + 1) + "行异常：" + ex.Message);
            //    }
            //}
            txtInfo.Text += sb.ToString();
        }

        protected void Button3_Click(object sender, EventArgs e)
        {
            int succCount = 0;
            int failCount = 0;
            int updateCount = 0;
            var sbFail = new StringBuilder();
            var serv = new Ali_OrdersB();
            var aliApiServ = new RunAliAPI();
            var sourceDt=new Ali_OrdersD().GetDataSetBySql("select * from TestOrder").Tables[0];
            foreach (DataRow dataRow in sourceDt.Rows)
            {
                var orderId = dataRow["orderId"].ToString();
                try
                {
                    
                    //根据订单号和ENid判断是否存在
                    var stu = new MODEL.Ali.GetAli_Orders();
                    stu.ORDERIDVALUE = orderId.Trim();
                    var oDt = serv.GetAli_Orders(ref stu);

                    if (oDt == null || oDt.Rows.Count <= 0)
                    {
                        //订单不存在， 新增
                        var resJson = aliApiServ.GetOrderInfo(orderId.Trim(), false, false);

                        var htFirst = JSON.Decode(resJson) as Hashtable;
                        if (htFirst["success"].ToString().ToLower() == "true")
                        {
                            var arrayList = htFirst["dataList"] as ArrayList;
                            if (arrayList != null)
                            {
                                foreach (Hashtable item in arrayList)
                                {
                                    var insStu = new MODEL.Ali.EditAli_Orders();
                                    insStu.ORDERID_VALUE = orderId.Trim();
                                    insStu.ENIDVALUE = item["snapshotId"];
                                    insStu.BizID_VALUE = item["bizId"];
                                    //insStu.RUIDVALUE = ruid;

                                    CommonClass.Common.InitDataEntity(htFirst, insStu.BizID_VALUE.ToString(), true,
                                        ref insStu);

                                    insStu.EFFECTIVE_VALUE = false;
                                    insStu.ACTION_VALUE = "Insert";
                                    insStu.EFFECTIVE_VALUE = false;

                                    int i = serv.EditAli_Orders(ref insStu);
                                    if (i > 0)
                                        succCount++;
                                    else
                                    {
                                        failCount++;
                                    }
                                }
                            }
                        }
                        else
                        {
                            failCount++;
                            sbFail.Append("根据订单号：" + orderId.Trim() + "获取订单信息失败！\r\n");
                        }
                    }
                    else
                    {
                        //订单存在   同步部分信息
                        foreach (DataRow dr in oDt.Rows)
                        {
                            var entity = new MODEL.Ali.EditAli_Orders
                            {
                                ACTION_VALUE = "Update",
                                ID_VALUE = dr["ID"].ToString()
                            };

                            var resJson = aliApiServ.GetOrderInfo(dr["OrderId"].ToString(), false, false);


                            var htFirst = JSON.Decode(resJson) as Hashtable;
                            if (htFirst["success"].ToString().ToLower() == "true")
                            {

                                CommonClass.Common.InitDataEntity(htFirst, dr["BizID"].ToString(),
                                    false, ref entity);

                                int i = serv.EditAli_Orders(ref entity);
                                if (i > 0)
                                    updateCount++;
                                else
                                {
                                    failCount++;
                                }
                            }
                            else
                            {
                                sbFail.Append("调用Ali接口失败;");
                            }
                        }

                    }
                    
                }
                catch (Exception ex)
                {
                    //ShowAlert(ex.Message);
                }
            }
            txtInfo.Text = string.Format("导入成功数量：{0}！\r\n导入失败数量：{1}\r\n更新数量：{2}\r\n{3}", succCount, failCount,
                        updateCount,
                        string.IsNullOrEmpty(sbFail.ToString()) ? string.Empty : sbFail.ToString());
        }
    }
}